package com.cdut.logistics.model.dao.businesses;

import java.util.Date;
import java.util.List;

import com.cdut.logistics.config.Constants;
import com.cdut.logistics.model.dao.datacentre.Area;
import com.cdut.logistics.model.dao.datacentre.Dict;
import com.cdut.logistics.model.dao.datacentre.School;
import com.cdut.logistics.model.dao.datacentre.User;
import com.cdut.logistics.model.entity.Model.Dorm;
import com.cdut.logistics.model.entity.Model.Repair_Detail;
import com.cdut.logistics.model.entity.Model.Repair_Order;
import com.cdut.logistics.model.entity.Model.Repair_Task;
import com.cdut.logistics.model.entity.Model.Suggestion;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class RepairDao {
   public static RepairDao  dao = new RepairDao();
   
   public Repair_Detail list_detail(int order_id)
   {
	   String sql = "select * from repair_detail where  order_id= ? and "+Constants.sql_add;
	   Repair_Detail list=Repair_Detail.dao.findFirst(sql,order_id);
	   return list;
   }
   public List<Repair_Order> list_order()
   {
	   String sql = "select * from repair_order where   "+Constants.sql_add;
	   List<Repair_Order> list=Repair_Order.dao.find(sql);
	   return list;
   }
   public List<Repair_Order> list_order_baoxiu()
   {
	   String sql = "select * from repair_order where  Auditing_status =0 and order_status =0 and "+Constants.sql_add;
	   List<Repair_Order> list=Repair_Order.dao.find(sql);
	   return list;
   }
   public List<Repair_Order> list_order_anpai()
   {
	   String sql = "select * from repair_order where Auditing_status =1 and arrangement_status =0 and "+Constants.sql_add;
	   List<Repair_Order> list=Repair_Order.dao.find(sql);
	   return list;
   }
   public List<Repair_Order> list_order_diaodu()
   {
	   String sql = "select * from repair_order where Auditing_status =1 and arrangement_status =1 and diaodu_status =0 and "+Constants.sql_add;
	   List<Repair_Order> list=Repair_Order.dao.find(sql);
	   return list;
   }
   public List<Repair_Order> list_order_result()
   {
	   String sql = "select * from repair_order where Auditing_status =1 and arrangement_status =1 and diaodu_status =1 and order_status = 0 and "+Constants.sql_add;
	   List<Repair_Order> list=Repair_Order.dao.find(sql);
	   return list;
   }
   public Repair_Order repair_order(int order_id)
   {
	   String sql = "select * from repair_order where id= ?  and "+Constants.sql_add;
	   Repair_Order list=Repair_Order.dao.findFirst(sql,order_id);
	   return list;
   }
   public List<Repair_Order> list_order(int user_id)
   {
	   String sql = "select * from repair_order where  user_id= ? and "+Constants.sql_add;
	   List<Repair_Order> list=Repair_Order.dao.find(sql,user_id);
	   return list;
   }
   public Repair_Order single_order(int order_id)
   {
	   String sql = "select * from repair_order where  id= ? and "+Constants.sql_add;
	   Repair_Order list=Repair_Order.dao.findFirst(sql,order_id);
	   return list;
   }
   public List<Record> list_record(int user_id)
   {
	   String sql = "SELECT o.*,d.*,o.id as oid,d.id as did"
                   +" FROM reapir_detail d LEFT JOIN repair_order o on o.id = d.order_id"
			       +" where o.active =1 and o.is_use= 1 and d.active =1 and d.is_use=1 and o.user_id=? ";
	   List<Record>  record = Db.find(sql,user_id);
	   return record;
   }
   public boolean is_task(int user_id)
   {
	   String sql ="select * from repair_task where repair_member =? and start_time <=? and end_time>=? and "+Constants.sql_add;
	   Repair_Task repair_task  =Repair_Task.dao.findFirst(sql,user_id,new Date(),new Date());
	   boolean is_task;
	   if(repair_task!=null)
	   {
		   is_task = true;
	   }else
	   {
		   is_task = false;
	   }
	   return is_task;
   }
   
   
   public boolean repair_save(int user_id,int school_id, int dorm_id, int area_id,int repair_type, String repair_text, String img_url,String video_url, String remarks,boolean is_task) {
       Repair_Order  repair_Order= new Repair_Order();
       repair_Order.set("user_id", user_id);
       repair_Order.set("created", new Date());
       repair_Order.set("remarks", remarks);
       if(is_task ==true)
       {
    	   repair_Order.set("Auditing_status", 1);
       }else
       {
    	   repair_Order.set("Auditing_status", 0);
       }      
       repair_Order.set("arrangement_status", 0);
       repair_Order.set("order_status", 0);
       repair_Order.set("is_use", 1);
       repair_Order.set("active", 1);
       boolean is_success =  repair_Order.save();
       
       if(!is_success) return false;
       
       Repair_Detail repair_Detail =  new Repair_Detail();
       repair_Detail.set("order_id", repair_Order.getInt("id"));
       repair_Detail.set("school_id", school_id);
       repair_Detail.set("dorm_id", dorm_id);
       repair_Detail.set("area_id", area_id);
       repair_Detail.set("repair_text", repair_text);
       repair_Detail.set("img_url", img_url);
       repair_Detail.set("video_url", video_url);
       repair_Detail.set("repair_type", repair_type);
       repair_Detail.set("is_use", 1);
       repair_Detail.set("active", 1);
       repair_Detail.set("created", new Date());
       
       boolean is_success1 =  repair_Detail.save();
       
       if(!is_success1) return false;
       
       return true;
       
   }
   public User user_detail(int user_id)
   {
	   User user = User.dao.findById(user_id);
	   return user;
   }
   
   public School school_detail(int school_id)
   {
	   School school = School.dao.findById(school_id);
	   return school;
   }
   
   public Dorm dorm_detail(int dorm_id)
   {
	   System.out.println(dorm_id);
	   Dorm dorm = Dorm.dao.findById(dorm_id);
	   return dorm;
   }
   public Area   area_single(int area_id)
   {
	   String sql = "select * from area where id= ?  and "+Constants.sql_add;
	   Area area = Area.dao.findFirst(sql,area_id);
	   return area;
   }
   public List<Area> area_detail(int school_id)
   {
	   String sql = "select * from area where school_id= ?  and "+Constants.sql_add;
	   List<Area> area = Area.dao.find(sql,school_id);
	   return area;
   }
   public Dict sys_dict_single(int id)
   {
	   String sql = "select * from sys_dict where id= ?  and "+Constants.sql_add;
	   Dict sys_dict = Dict.dao.findFirst(sql,id);
	   return sys_dict;
   }
   
   public List<Dict> sys_dict_detail(int type)
   {
	   String sql = "select * from sys_dict where type= ?  and "+Constants.sql_add;
	   List<Dict> sys_dict = Dict.dao.find(sql,type);
	   return sys_dict;
   }
   public boolean repair_cancel(int order_id)
   {
	   Repair_Order repair_Order = Repair_Order.dao.findById(order_id);
	   if(repair_Order!=null)
	   {
		   repair_Order.set("order_status",-1);
		   repair_Order.set("is_use", 0);
		   boolean is_success = repair_Order.update();
		   return is_success;
	   }
	   return false;
   }
   public boolean repair_auditing_agree(int order_id)
   {
	   Repair_Order repair_Order = Repair_Order.dao.findById(order_id);
	   if(repair_Order!=null)
	   {
		   repair_Order.set("Auditing_status",1);
		   boolean is_success = repair_Order.update();
		   return is_success;
	   }
	   return false;
   }
   public boolean repair_auditing_disagree(int order_id,String remarks)
   {
	   Repair_Order repair_Order = Repair_Order.dao.findById(order_id);
	   if(repair_Order!=null)
	   {
		   repair_Order.set("Auditing_status",-1);
		   
		   boolean is_success = repair_Order.update();
		   if(!is_success) return false;
		    
		   String sql = "select * from repair_detail where order_id = ? and "+Constants.sql_add;
		   Repair_Detail repair_Detail = Repair_Detail.dao.findFirst(sql,repair_Order.getInt("id"));
		   if(repair_Detail !=null)
		   {
			   repair_Detail.set("remarks", remarks);
			   boolean is_success2 = repair_Detail.update();
			   return is_success2;
		   }
		   return false;
	   }
	   return false;
   }
   
   public boolean repair_task(int leader_id,int school_id, int area_id,String repair_member,String start_time,String end_time,String remarks)
   {
	   Repair_Task repair_Task =  new Repair_Task();
	   repair_Task.set("task_leader", leader_id);
	   repair_Task.set("school_id", school_id);
	   repair_Task.set("area_id", area_id);
	   repair_Task.set("repair_member", repair_member);
	   repair_Task.set("start_time", start_time);
	   repair_Task.set("end_time", end_time);
	   repair_Task.set("remarks", remarks);
	   repair_Task.set("is_use", 1);
	   repair_Task.set("active", 1);
	   repair_Task.set("created", new Date());
       boolean is_success = repair_Task.save();
       return is_success;
	   
   }
   
   public boolean repair_arrange(int leader_id,Repair_Detail repair_Detail )
   {
	   repair_Detail.set("repair_leader", leader_id);
	   boolean is_success = repair_Detail.update();
	   return is_success;
   }
   
   public boolean repair_diaodu(String repair_member,Repair_Detail repair_Detail )
   {
	   repair_Detail.set("repair_member", repair_member);
	   boolean is_success = repair_Detail.update();
	   return is_success;
   }
   public boolean repair_result(Repair_Order repair_Order )
   {
	   repair_Order.set("order_status", 2);
	   boolean is_success = repair_Order.update();
	   return is_success;
   }
   
   public boolean repair_suggestion(int order_id,int user_id, String content )
   {
	   Suggestion suggestion = new Suggestion();
	   suggestion.set("order_id", order_id);
	   suggestion.set("user_id", user_id);
	   suggestion.set("content", content);
	   suggestion.set("type", 1);
	   suggestion.set("created", new Date());
	   suggestion.set("is_use",1);
	   suggestion.set("active",1);
	   boolean is_success = suggestion.save();
	   return is_success;
   }
   
   public boolean repair_advice(int user_id, String content )
   {
	   Suggestion suggestion = new Suggestion();
	   suggestion.set("user_id", user_id);
	   suggestion.set("content", content);
	   suggestion.set("type", 0);
	   suggestion.set("created", new Date());
	   suggestion.set("is_use",1);
	   suggestion.set("active",1);
	   boolean is_success = suggestion.save();
	   return is_success;
   }
   
  
}
